package com.gisuni.sddc.platformManager.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.dao.util.cri.SqlExpression;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import com.gisuni.sddc.platformManager.model.Log;
import com.gisuni.sddc.platformManager.model.Organization;
import com.gisuni.sddc.platformManager.model.RoleInfo;
import com.gisuni.sddc.platformManager.model.UserInfo;
import com.gisuni.sddc.util.StringUtil;

@IocBean
public class LogDao {
	
	@Inject
	private Dao gisplatform;
  
 

	public String queryBaseLogAll(int from, int to, String userName, String startDate, String endDate) throws Exception {
		 //TODO userName
		
		if(StringUtil.isNullOrEmpty(startDate)){
			startDate = "2010-01-01";
		}
		if(StringUtil.isNullOrEmpty(endDate)){
			endDate = "2050-01-01";
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		Date start = sdf.parse(startDate);
		Date end = sdf.parse(endDate);
		
		 
		Cnd cnd = Cnd.where("CREATE_TIME",">=",start).and("CREATE_TIME","<=",end);
 
		Pager pager = new Pager();
		pager.setPageNumber(from);
		pager.setPageSize(to);
		pager.setRecordCount(to);
		List<Log> list = gisplatform.query(Log.class, cnd, pager);
		if(list!=null && list.size()>0){
			JSONArray result=new JSONArray();
		    for(Log log : list){
		    	JSONObject ret=new JSONObject();
		    	ret.put("id", log.getId());
		    	ret.put("userName", log.getUserName());
		    	ret.put("type", log.getType());
		    	ret.put("info", log.getType());
		    	ret.put("status", log.getType());
		    	ret.put("createTime", log.getCreateTime());
		    	ret.put("updateTime", log.getUpdateTime());
		    	result.put(ret);
		    }
		    return result.toString();
		    	 
		}
		
		return null;
	}
	

	public int queryBaseLogCount(String userName, String startDate, String endDate) throws Exception {

		 //TODO userName
		
		if(StringUtil.isNullOrEmpty(startDate)){
			startDate = "2010-01-01";
		}
		if(StringUtil.isNullOrEmpty(endDate)){
			endDate = "2050-01-01";
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		Date start = sdf.parse(startDate);
		Date end = sdf.parse(endDate);
		Cnd cnd = Cnd.where("CREATE_TIME",">=","to_date('"+startDate+"',yyyy-MM-dd)").and("CREATE_TIME","<=","to_date('"+endDate+"',yyyy-MM-dd)");
		
		List<Log> listSec =gisplatform.query(Log.class, cnd, null);
		
		if(listSec!=null && listSec.size()>0){
			return listSec.size();
		}else{
			return 0;
		}
	}

	public boolean addLog(com.gisuni.sddc.platformManager.model.Log logs) {
		
		logs = gisplatform.insert(logs);
		
		if(logs!=null){
			return true;
		}
		return false;
	}
 
	
	public String findData(String selectSql) {
		Sql sql = Sqls.create(selectSql);
	    sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql)
					throws SQLException {
				JSONArray result=new JSONArray();
				int numberOfColumns = rs.getMetaData().getColumnCount();
			     while (rs.next()){
			    	 JSONObject ret=new JSONObject();
						// 遍历查询结果字段
						for (int i = 0; i < numberOfColumns; i++) {
							Object obj = rs.getString(i + 1);
							ret.put(rs.getMetaData().getColumnName(i+1).trim(), obj);
						}
			    	 result.put(ret);
			     }
			     return result.toString();
			}
		});
	    gisplatform.execute(sql);
	    return  sql.getObject(String.class);
		 
	}
	
	public int findRows(String selectSql) {
		Sql sql = Sqls.create(selectSql);
	    sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql)
					throws SQLException {
				int row =0;
			     while (rs.next()){
			    	 row = Integer.parseInt( rs.getString("sizes"));
			     }
			     return row;
			}
		});
	    gisplatform.execute(sql);
	    return  sql.getObject(int.class);
		 
	}
	
 
 
}
